package com.hl.sfzy.controller.myController;


import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.my.MyNotify;
import com.hl.sfzy.service.myService.MyNotifyService;
import com.hl.sfzy.utils.FileUpUtils;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("api/my/notify")
public class MyNotifyController {
    @Autowired
    private MyNotifyService myNotifyService;



    @Value("${article.banner.img.dir.path}")
    private String banner_url;


    @GetMapping("getNotifys")
    public R notifys(int pageNo,int pageSize){
        PageInfo<MyNotify> myNotifyPageInfo = myNotifyService.myNotifyList(pageNo, pageSize);
        System.out.println(myNotifyPageInfo);
        return R.success(myNotifyPageInfo);
    }


    @PostMapping("addNotify")
    public R<String> addNotify(@RequestBody MyNotify myNotify){
        System.out.println(myNotify);
        int i = myNotifyService.addMyNotify(myNotify);
        if (i > 0){
            return R.success("添加成功");
        }else {
            return R.failure("添加失败");
        }
    }



    @DeleteMapping("deleteNotify/{id}")
    public R<String> deleteNotify(@PathVariable("id") int id){
        int i = myNotifyService.deleteMyNotify(id);
        if (i > 0){
            return R.success("删除成功");
        }else {
            return R.failure("删除失败");
        }
    }

    @PostMapping("/fileup")
    public R<String> articleImgUpload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()){
            return R.failure("文件为空");
        }
        String fileName = FileUpUtils.fileUp(file, banner_url);
        return R.success(banner_url +"/"+fileName);
    }


    @PutMapping("updateNotify")
    public R<String> updateNotify(@RequestBody MyNotify myNotify){
        int i = myNotifyService.updateMyNotify(myNotify);
        if (i > 0) {
            return R.success("更新成功");
        }
        return R.failure("更新失败");
    }
}
